<?php
require_once('config.php');

//bulan
$bln=date("m");
$bulan=array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
$bulan_sekarang=$bulan[$bln-1];
//tanggal
$tanggal=explode("-",date("Y-m-d"));
for($x=$tanggal[2];$x>=1;$x--){
$tgl[$x-1]="$tanggal[0]-$tanggal[1]-$x";
}
//data
for($t=0;$t<count($tgl);$t++){
	$sql_rts=mysql_query("SELECT SUM(tb_rts.jumlah) as rts_total FROM tb_rts 
													LEFT JOIN tb_produksi ON (tb_rts.id_prod=tb_produksi.id_produksi)
													WHERE tb_rts.tgl = '$tgl[$t]'") or die(mysql_error());
	$sql_rtu=mysql_query("SELECT SUM(tb_rtu.jumlah) as rtu_total FROM tb_rtu 
													LEFT JOIN tb_produksi ON (tb_rtu.id_prod=tb_produksi.id_produksi)
													WHERE tb_rtu.tgl = '$tgl[$t]'");
	$sql_limbah=mysql_query("SELECT SUM(limbah_prod) as limbah_prod FROM tb_produksi
													WHERE tgl_prod = '$tgl[$t]'");
	$d=mysql_fetch_array($sql_rts);
	$d1=mysql_fetch_array($sql_rtu);
	$d2=mysql_fetch_array($sql_limbah);
	/*
	if(is_null($d['rts_total'])){
		$rts = 0;
	}else{
		$rts = $d['rts_total']+0;
	}
	*/
	$rets[]=(int)$d['rts_total'];
	$retu[]=(int)$d1['rtu_total'];
	$limbah[]=(int)$d2['limbah_prod'];
	
}
$sql_stok=mysql_query("SELECT * FROM tb_kode_bb");
while($stok=mysql_fetch_array($sql_stok)){
	for($t=0;$t<count($tgl);$t++){
		$sql_d_stok = mysql_query("SELECT SUM(jumlah) as data_stok FROM tb_bb WHERE kode_bb = '$stok[kode]' AND DATE(tgl_masuk) = '$tgl[$t]'");
		$sql_d_stok_out = mysql_query("SELECT SUM(jumlah) as data_stok_out FROM tb_bb_keluar WHERE kode_bb = '$stok[kode]' AND DATE(tgl_keluar) = '$tgl[$t]'");
		$d_stok=mysql_fetch_array($sql_d_stok);
		$d_stok_out=mysql_fetch_array($sql_d_stok_out);
		$datak[] = (int)$d_stok['data_stok'];
		$datak_out[] = (int)$d_stok_out['data_stok_out'];
		
	}
	$data_stok[] = array("name"=>$stok['nama'],"data"=>$datak);
	unset($datak);
	$data_stok_out[] = array("name"=>$stok['nama'],"data"=>$datak_out);
	unset($datak_out);
	
}
?>
<script type="text/javascript">
var chart; // globally available
$(document).ready(function() {
	chart = new Highcharts.Chart({
		chart: {
		renderTo: 'container',
		defaultSeriesType: 'spline'
		},
		title: {
			text: 'Ready To Use'
		},
		subtitle: {
			text: '<?php echo $bulan_sekarang; ?>'
		},
		xAxis: {
			categories: <?php echo json_encode($tgl) ?>,
			labels: {
				rotation: -45,
				align: 'right',
				style: {
					 font: 'normal 7px Verdana, sans-serif'
				}
			},
			title: {
				enabled: false
			}
		},
		yAxis: {
			min: 0,
			title: {
				text: 'Jumlah Barang'
			}
		},
		tooltip: {
			formatter: function() {
				return ''+
					this.x +': '+ this.y +' KARDUS';
			}
		},
		plotOptions: {
			column: {
				pointPadding: 0.2,
				borderWidth: 0
			}
		},
			series: [{
			name: 'RTU',
			data: <?php echo json_encode($retu) ?>
	
		}]
	});
	//chart 2
		chart = new Highcharts.Chart({
		chart: {
		renderTo: 'container2',
		defaultSeriesType: 'spline'
		},
		title: {
			text: 'Ready To Sale'
		},
		subtitle: {
			text: '<?php echo $bulan_sekarang; ?>'
		},
		xAxis: {
			categories: <?php echo json_encode($tgl) ?>,
			labels: {
				rotation: -45,
				align: 'right',
				style: {
					 font: 'normal 7px Verdana, sans-serif'
				}
			},
			title: {
				enabled: false
			}
		},
		yAxis: {
			min: 0,
			title: {
				text: 'Jumlah Barang'
			}
		},
		tooltip: {
			formatter: function() {
				return ''+
					this.x +': '+ this.y +' KARDUS';
			}
		},
		plotOptions: {
			column: {
				pointPadding: 0.2,
				borderWidth: 0
			}
		},
			series: [{
			name: 'RTS',
			data: <?php echo json_encode($rets) ?>
	
		}]
	});
	//3
	chart = new Highcharts.Chart({
		chart: {
			renderTo: 'container3',
			defaultSeriesType: 'column'
		},
		title: {
			text: 'STOK BAHAN BAKU MASUK'
		},
		subtitle: {
			text: '<?php echo $bulan_sekarang; ?>'
		},
		xAxis: {
			categories: <?php echo json_encode($tgl) ?>,
			labels: {
				rotation: -45,
				align: 'right',
				style: {
					 font: 'normal 7px Verdana, sans-serif'
				}
			},
		},
		yAxis: {
			min: 0,
			title: {
				text: ''
			}
		},
		legend: {
			layout: 'vertical',
			backgroundColor: '#FFFFFF',
			align: 'left',
			verticalAlign: 'top',
			x: 100,
			y: 70,
			floating: true,
			shadow: true
		},
		tooltip: {
			formatter: function() {
				return ''+
					this.x +': '+ this.y +'';
			}
		},
		plotOptions: {
			column: {
				pointPadding: 0.2,
				borderWidth: 0
			}
		},
			series: <?php echo json_encode($data_stok) ?>
	});
//chart 4
	chart = new Highcharts.Chart({
		chart: {
			renderTo: 'container4',
			defaultSeriesType: 'column'
		},
		title: {
			text: 'STOK BAHAN BAKU KELUAR'
		},
		subtitle: {
			text: '<?php echo $bulan_sekarang; ?>'
		},
		xAxis: {
			categories: <?php echo json_encode($tgl) ?>,
			labels: {
				rotation: -45,
				align: 'right',
				style: {
					 font: 'normal 7px Verdana, sans-serif'
				}
			},
		},
		yAxis: {
			min: 0,
			title: {
				text: ''
			}
		},
		legend: {
			layout: 'vertical',
			backgroundColor: '#FFFFFF',
			align: 'left',
			verticalAlign: 'top',
			x: 100,
			y: 70,
			floating: true,
			shadow: true
		},
		tooltip: {
			formatter: function() {
				return ''+
					this.x +': '+ this.y +'';
			}
		},
		plotOptions: {
			column: {
				pointPadding: 0.2,
				borderWidth: 0
			}
		},
			series: <?php echo json_encode($data_stok_out) ?>
	});
//chart 5
		chart = new Highcharts.Chart({
		chart: {
		renderTo: 'container5',
		defaultSeriesType: 'spline'
		},
		title: {
			text: 'Limbah Produksi'
		},
		subtitle: {
			text: '<?php echo $bulan_sekarang; ?>'
		},
		xAxis: {
			categories: <?php echo json_encode($tgl) ?>,
			labels: {
				rotation: -45,
				align: 'right',
				style: {
					 font: 'normal 7px Verdana, sans-serif'
				}
			},
			title: {
				enabled: false
			}
		},
		yAxis: {
			min: 0,
			title: {
				text: 'Jumlah Barang'
			}
		},
		tooltip: {
			formatter: function() {
				return ''+
					this.x +': '+ this.y +' ITEM';
			}
		},
		plotOptions: {
			column: {
				pointPadding: 0.2,
				borderWidth: 0
			}
		},
			series: [{
			name: 'LIMBAH PRODUKSI',
			data: <?php echo json_encode($limbah) ?>
	
		}]
	});
	
});

</script>

<!-- box / title -->
					<div class="title">
						<h5>&nbsp;</h5>						
					</div>
					<!-- end box / title -->
					<div class="table">
						<p>
						<h2 align="center" style="padding-top:10px">SELAMAT DATANG DI APLIKASI ERP PT ATHIS</h2>
						</p>
						<table width="100%" border="0" cellpadding="3" cellspacing="3">
						  <tr>
						    <td valign="top" align="center">
								<!--<div class="head-menu">Debet</div>-->
								<div id="container" style="width:500px;height:300px;margin:5px 0px 5px 0px;"></div>
							</td>
						    <td valign="top">
								<!--<div class="head-menu">Debet</div>-->
								<div id="container2" style="width:500px;height:300px;margin:5px 5px 5px 5px;"></div>
							</td>
						  </tr>
						  <tr>
							<td valign="top" colspan="2" align="center">
								<div id="container5" style="width:500px;height:300px;margin:5px 5px 5px 5px;"></div>
							</td>	
						  </tr>
						  <tr>
						    <td valign="top">
								<!--<div class="head-menu">Debet</div>-->
								<div id="container3" style="width:500px;height:300px;margin:5px 5px 5px 5px;"></div>
							</td>
						    <td valign="top">
								<!--<div class="head-menu">Debet</div>-->
								<div id="container4" style="width:500px;height:300px;margin:5px 5px 5px 5px;"></div>
							</td>
						  </tr>
						</table>
					</div>